import axios from "axios"
import {ElMessage} from "element-plus";
import router from "@/router";
import {serverPath} from "@/utils/path";

const httpInstance = axios.create({
    baseURL: `${serverPath}`,
    timeout: 50000000,
    withCredentials: true
})

// 添加请求拦截器
httpInstance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
httpInstance.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么

    // 没有返回 Result 对象
    if (response.data.code === undefined) {
        return response
    }

    if (response.data.code === 510) {
        // 登陆过期
        if (response.data.msg.indexOf("token") !== -1) {
            document.cookie = "satoken=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
            router.push("/")
            return response

            let currentURL = window.location.href;
            let prefix = currentURL.indexOf("https://") !== -1 ? "https://" : "http://";
            currentURL = currentURL.substring(prefix.length)
            currentURL = currentURL.substring(0, currentURL.indexOf("/"))
            window.location.href = prefix + currentURL
        }else {
            ElMessage({
                type:"error",
                message:response.data.msg
            })
        }
    }

    //
    if (response.data.code === 502) {
        ElMessage({
            type: "error",
            message: response.data.msg
        })
        return response
    }

    // 弹出后端响应的内容
    if(response.data.code === 500){
        ElMessage({
            type: "error",
            message: response.data.msg
        })
        return response
    }

    // if(response.data.code === 200){
    //     ElMessage({
    //         type: "success",
    //         message: response.data.data
    //     })
    //     return response
    // }


    // if(typeof response.data.code === "undefined"){
    //     return response
    // }
    // if (response.data.code !== 200) {
    //     if (response.data.msg.indexOf("token") !== -1) {
    //         ElMessage({
    //             showClose: true,
    //             message: "登陆过期重新登录",
    //             type: 'error',
    //         })
    //         document.cookie = "satoken" + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    //         let currentURL = window.location.href;
    //         let prefix = currentURL.indexOf("https://") !== -1 ? "https://" : "http://";
    //         console.log(prefix)
    //         currentURL = currentURL.substring(prefix.length)
    //         currentURL = currentURL.substring(0, currentURL.indexOf("/"))
    //         console.log(prefix + currentURL)
    //         window.location.href = prefix + currentURL
    //         return response;
    //     }
    //
    //     ElMessage({
    //         showClose: true,
    //         message: response.data.msg,
    //         type: 'error',
    //     })
    // }
    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
});

export default httpInstance